package com.zcmob.web.admanagement.servlet;

import java.io.File;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcmob.web.admanagement.db.AppDao;
import com.zcmob.web.admanagement.util.FileUtils;
import com.zcmob.web.admanagement.util.PropsUtils;
import com.zcmob.web.admanagement.util.StringUtils;
import com.zcmob.web.admanagement.vo.AdUser;
import com.zcmob.web.admanagement.vo.AppBid;
import com.zcmob.web.admanagement.vo.AppInfo;
import com.zcmob.web.admanagement.vo.ServiceType;

public class NewAppServlet extends ZcMobServlet {

    /**
     * 
     */
    private static final long serialVersionUID = -5341963273170979039L;

    @Override
    protected void handle(HttpServletRequest req, HttpServletResponse response) {
        try {
            String apk_file = req.getParameter("apk_file");
            String image_name1 = req.getParameter("image_name1");
            String image_name2 = req.getParameter("image_name2");
            String image_name3 = req.getParameter("image_name3");
            String packagename = req.getParameter("packagename");
            String appname = req.getParameter("appname");
            String versionname = req.getParameter("versionname");
            String filesize = req.getParameter("filesize");
            String category = req.getParameter("category");
            String contact_email = ((AdUser)req.getSession().getAttribute("aduser")).getContact_email();
            String contact_phone = ((AdUser)req.getSession().getAttribute("aduser")).getContact_phone();
            String summary = req.getParameter("summary");
            String description = req.getParameter("description");
            String iconhttppath = req.getParameter("iconhttppath");
            String active = req.getParameter("hidden_active");
            String tiyan = req.getParameter("hidden_tiyan");
            String tiyan_type = req.getParameter("hidden_tiyan_type");
            String adv_price = req.getParameter("hidden_adv_price");
            int active_time = Integer.valueOf(req.getParameter("active_time"));
            int active_type_id = Integer.valueOf(req.getParameter("active_type_id"));
            String status = req.getParameter("status");
            
            
            // 复制临时apk和jpg到正式文件夹，成功后删除临时文件
            String tmpAPKpath = PropsUtils.getProperty("apktmpserverpath") + apk_file;
            String tmpAPKDir = PropsUtils.getProperty("apktmpserverpath") + apk_file.substring(0,
                    apk_file.length() - 4) + "Dir";
            FileUtils.copyFile(tmpAPKpath, 
                    PropsUtils.getProperty("apkserverpath"), packagename + ".apk");
            
            String tmpIconpath = PropsUtils.getProperty("icontmpserverpath") + 
                    iconhttppath.substring(iconhttppath.lastIndexOf("/") + 1, iconhttppath.length());
            FileUtils.copyFile(tmpIconpath, 
                    PropsUtils.getProperty("iconserverpath"), packagename + ".png");
            
            String tmpJpg1Path = PropsUtils.getProperty("jpgtmpserverpath") + image_name1;
            FileUtils.copyFile(tmpJpg1Path, 
                    PropsUtils.getProperty("iconserverpath"), packagename + "_1.jpg");
            
            String tmpJpg2Path = PropsUtils.getProperty("jpgtmpserverpath") + image_name2;
            FileUtils.copyFile(tmpJpg2Path, 
                    PropsUtils.getProperty("iconserverpath"), packagename + "_2.jpg");
            
            String tmpJpg3Path = PropsUtils.getProperty("jpgtmpserverpath") + image_name3;
            FileUtils.copyFile(tmpJpg3Path, 
                    PropsUtils.getProperty("iconserverpath"), packagename + "_3.jpg");
           
            AppInfo appinfo = new AppInfo();
            
            appinfo.setApk_size(Long.valueOf(filesize));
            appinfo.setAppname(appname);
            appinfo.setCategory(category);
            appinfo.setContact_email(contact_email);
            appinfo.setContact_phone(contact_phone);
            appinfo.setDescription(description);
            appinfo.setSummary(summary);
            appinfo.setPackagename(packagename);
            appinfo.setPic_number(3);
            appinfo.setVersion(versionname);
            appinfo.setActive_time(active_time);
            appinfo.setActive_type_id(active_type_id);
            appinfo.setStatus(status);
            
            AdUser aduser = (AdUser)req.getSession().getAttribute("aduser");
            appinfo.setAd_user_id(aduser.getId());
            
            if (!StringUtils.isNullOrEmpty(tiyan)) {
                ServiceType serviceType = AppDao.getInstance().getServiceTypeByid(Integer.valueOf(tiyan_type));
                appinfo.setEffect_running_time(serviceType.getEffect_running_time());
                appinfo.setMax_times(serviceType.getMax_times());
                appinfo.setDuration(serviceType.getDuration());
            }
            
            AppDao.getInstance().insertApp(appinfo);
            
            if (appinfo.getId() > 0) {
                AppBid appBid = new AppBid();
                appBid.setAd_unit_bid(Double.valueOf(active));
                appBid.setAdv_price(Double.valueOf(adv_price));
                appBid.setAppid(appinfo.getId());
                appBid.setServiceid(1);
                appBid.setUnit_bid(Double.valueOf(active) / 3);
                AppDao.getInstance().insertAppBid(appBid);
                
                if (!StringUtils.isNullOrEmpty(tiyan)) {
                    
                    AppBid tityanappBid = new AppBid();
                    tityanappBid.setAd_unit_bid(Double.valueOf(tiyan));
                    tityanappBid.setAdv_price(Double.valueOf(adv_price));
                    tityanappBid.setAppid(appinfo.getId());
                    //此处有坑，tiyan_type是从页面hardcode跟数据库一致的，如果数据库的id变了，会导致数据不一致
                    tityanappBid.setServiceid(Integer.valueOf(tiyan_type));
                    tityanappBid.setUnit_bid(Double.valueOf(tiyan) / 2);
                    AppDao.getInstance().insertAppBid(tityanappBid); 
                }
            }
            
            new File(tmpAPKpath).delete();
            new File(tmpAPKDir).delete();
            new File(tmpIconpath).delete();
            new File(tmpJpg1Path).delete();
            new File(tmpJpg2Path).delete();
            new File(tmpJpg3Path).delete();
            RequestDispatcher rd = req.getRequestDispatcher("s_applist");
            rd.forward(req, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
}
